package com.danger.Model.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.danger.Activity.R;
import com.danger.Util.ImageLoaderUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by 俊 on 2016/5/31.
 */
public class ImageAdapter extends BaseAdapter {

    private List<String> mImgPaths;
    private LayoutInflater inflater;
    private static Set<String> mSelectedImg = new HashSet<String>();
    private Context context;

    public ImageAdapter(Context context, List<String> mDatas) {
        this.mImgPaths = mDatas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mImgPaths.size();
    }

    @Override
    public Object getItem(int position) {
        return mImgPaths.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_gridview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //重置状态
        holder.item_imageView.setBackgroundResource(R.mipmap.no_photo);
        holder.item_select_imageButton.setImageResource(R.mipmap.unchoose);
        holder.item_imageView.setColorFilter(null);
//        ImageLoader.getInstance(5, ImageLoader.Type.LIFO).loadImage(mImgPaths.get(position), holder.item_imageView);
        new ImageLoaderUtil(holder.item_imageView,mImgPaths.get(position),context).display();
        holder.item_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //已经被选择
                if (mSelectedImg.contains(mImgPaths.get(position))) {
                    mSelectedImg.remove(mImgPaths.get(position));
                    holder.item_imageView.setColorFilter(null);
                    holder.item_select_imageButton.setImageResource(R.mipmap.unchoose);
                } else {
                    mSelectedImg.add(mImgPaths.get(position));
                    holder.item_imageView.setColorFilter(Color.parseColor("#77000000"));
                    holder.item_select_imageButton.setImageResource(R.mipmap.choose);
                }

            }
        });
        if (mSelectedImg.contains(mImgPaths.get(position))) {
            holder.item_imageView.setColorFilter(Color.parseColor("#77000000"));
            holder.item_select_imageButton.setImageResource(R.mipmap.choose);
        }
        return convertView;
    }

    private static class ViewHolder {
        ImageView item_imageView;
        ImageButton item_select_imageButton;

        public ViewHolder(View view) {
            item_imageView = (ImageView) view.findViewById(R.id.item_imageView);
            item_select_imageButton = (ImageButton) view.findViewById(R.id.item_select_imageButton);
        }
    }
}
